package com.anjubao.parking.municipal.file.controller;

import com.anjubao.parking.municipal.basezone.dto.Base64Dto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.anjubao.parking.municipal.basezone.dto.FileUploadResDTO;
import com.anjubao.parking.municipal.basezone.feign.FileFeignService;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.file.service.FileServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;

/**
 * 基础服务类--文件上传 文件上传API接口
 * 
 * @author anjubao
 */
@RestController
@RequestMapping("/file")
@Slf4j
@Api(value = "Parkinglot-file模块", tags = { "文件服务接口" })
public class FileController implements FileFeignService {

	@Autowired
	private FileServiceImpl fileServiceImpl;

	/**
	 * 文件上传接口
	 * 
	 * @param file
	 * @return
	 */
	@ApiOperation(value = "3.1.1图片信息上报接口")
	@ApiResponses(value = { @ApiResponse(code = 5000, message = "操作失败") })
	@PostMapping("/upload")
	public ApiResult<FileUploadResDTO> upload(@ApiParam(value = "图片文件", required = true) @RequestParam("fileName") MultipartFile file) {
		log.info("file文件大小长度 {}", file.getSize());
		if (file.getSize() == 0) {
			return new ApiResult<FileUploadResDTO>(5000, "文件大小不能为0KB");
		}
		try {
			return ApiResult.build(2000, "操作成功", new FileUploadResDTO(fileServiceImpl.uploadFile(file)));
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult<FileUploadResDTO>(5000, e.getMessage());
		}

	}

	/**
	 * base64上传接口
	 * 
	 * @param base64
	 * @return
	 */
	@Override
	@ApiOperation(value = "3.1.2图片base64信息上报接口")
	@ApiResponses(value = { @ApiResponse(code = 5000, message = "操作失败") })
	@PostMapping("/uploadBase64")
	public ApiResult<FileUploadResDTO> uploadBase64(@ApiParam(value = "图片文件base64", required = true) @RequestParam("base64") String base64) {
        System.out.println(base64);
		log.info("base64长度 {}", base64.length());
		if (base64.length() == 0) {
			return new ApiResult<FileUploadResDTO>(5000, "图片文件base64不能为0");
		}
		try {
			return ApiResult.build(2000, "操作成功", new FileUploadResDTO(fileServiceImpl.uploadBase64(base64)));
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult<FileUploadResDTO>(5000, e.getMessage());
		}
	}


    /**
     * base64上传接口
     *
     * @param base64Dto
     * @return
     */
    @Override
    @ApiOperation(value = "3.1.3图片base64信息上报接口")
    @ApiResponses(value = { @ApiResponse(code = 5000, message = "操作失败") })
    @PostMapping("/Base64")
    public ApiResult<FileUploadResDTO> Base64(@RequestBody Base64Dto base64Dto) {
        System.out.println(base64Dto.getBase64());
        log.info("base64长度 {}", base64Dto.getBase64().length());
        if (base64Dto.getBase64().length() == 0) {
            return new ApiResult<FileUploadResDTO>(5000, "图片文件base64不能为0");
        }
        try {
            return ApiResult.build(2000, "操作成功", new FileUploadResDTO(fileServiceImpl.uploadBase64(base64Dto.getBase64())));
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<FileUploadResDTO>(5000, e.getMessage());
        }
    }
}
